package com.ruoyi.cms.service;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.util.CmsConstants;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service("adult")
public class AdultService {


    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IArticleService articleService;



    private Cache<String,Object> siteInfoCache= CacheUtil.newTimedCache(1000*60*1);//10分钟有效时间，过期后重新数据库取值


    /**
     * 清空前台缓存
     */
    public void clearCache(){
        siteInfoCache.clear();
    }

    /**
     * 首页的文章列表
     * @return
     */
    public List<Article> ArticleByCateg(String categoryId,Integer limit){
        String KEY_INDEX_LIST=categoryId+limit;

        List<Article> list=( List<Article>)siteInfoCache.get(KEY_INDEX_LIST,false);
        if(list==null){
            Article form=new Article();
            form.setCategoryId(categoryId);
            PageHelper.startPage(1, limit);
            list=articleService.selectArticleList(form );
            siteInfoCache.put(KEY_INDEX_LIST,list);
        }
        return list;
    }
    /**
     * 首页的文章列表
     * @return
     */
    public List<Article> ArticleByCategPage(String categoryId,Integer page ,Integer limit){
        String KEY_INDEX_LIST=categoryId+"page"+page+"limit"+limit;


        List<Article> list=( List<Article>)siteInfoCache.get(KEY_INDEX_LIST,false);
        if(list==null){
            Article form=new Article();
            form.setCategoryId(categoryId);
            PageHelper.startPage(page, limit);
            list=articleService.selectArticleList(form );
            siteInfoCache.put(KEY_INDEX_LIST,list);
        }
        return list;
    }

    /**
     * 首页的文章列表
     * @return
     */
    public List<Article> ArticleByCategPage(String categoryId,Integer page ,String article_region,Integer limit){
        String KEY_INDEX_LIST=categoryId+"page"+page+"article_region"+article_region+"limit"+limit;


        List<Article> list=( List<Article>)siteInfoCache.get(KEY_INDEX_LIST,false);
        if(list==null){
            Article form=new Article();
            form.setCategoryId(categoryId);
            form.setArticleRegion(article_region);
            PageHelper.startPage(page, limit);
            list=articleService.selectArticleList(form );
            siteInfoCache.put(KEY_INDEX_LIST,list);
        }
        return list;
    }
    /**
     * 首页的文章列表
     * @return
     */
    public Article nextArticle(String categoryId,String ArticleId){
        String KEY_INDEX_LIST=categoryId+ArticleId;

        Article list=( Article)siteInfoCache.get(KEY_INDEX_LIST,false);
        if(list==null){
            Article form=new Article();
            form.setCategoryId(categoryId);
            form.setId(ArticleId);
            list=articleService.nextArticle(form);
            siteInfoCache.put(KEY_INDEX_LIST,list);
        }
        return list;
    }


}
